package gui.jtab.common;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JPanel;

import textures.viewer.JPanelViewImage;
import utils.files.FileManager;
import event.notifyTexture.NotifyTexture;
import gui.GLJPanels.GLJPanelNW;

public class JTabTextureOptions extends JPanel {

	private static final long serialVersionUID = 1L;

	private String texturesFolder;

	private JCheckBox textureCheckBox;
	
	private JCheckBox textureBlendedCheckBox;

	private JButton loadTextureButton;

	private JPanelViewImage principalViewer;

	private ArrayList<JPanelViewImage> texturePanels;

	private NotifyTexture notifyTextureManager;
	
	private GLJPanelNW canvas;

	private int nImages;

	private int x = 5;

	private int y = 6;

	private int xStart = 50;

	private int yStart = 250;

	private int size = 30;

	public JTabTextureOptions(String folder, GLJPanelNW canvas) {
		this.canvas = canvas;
		nImages = 0;
		texturesFolder = folder;
		texturePanels = new ArrayList<JPanelViewImage>(x * y);
		initTexturesViewers();
		initComponents();
	}

	private void initTexturesViewers() {
		File directory = new File(texturesFolder);
		File[] textures = directory.listFiles();
		for (int i = 0; i < y; i++) {
			for (int j = 0; j < x; j++) {
				JPanelViewImage panel = new JPanelViewImage();
				panel
						.setBorder(javax.swing.BorderFactory
								.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
				panel.setMinimumSize(new java.awt.Dimension(size, size));
				panel.setPreferredSize(new java.awt.Dimension(size, size));
				panel.addMouseListener(new java.awt.event.MouseAdapter() {
					public void mousePressed(java.awt.event.MouseEvent evt) {
						jPanelMousePressed(evt);
					}
				});
				add(panel);
				panel.setBounds(xStart + j * size, yStart + i * size, size,
						size);
				int index = i * x + j;
				if (textures!=null&&index < textures.length) {
					panel.createImage(textures[index].getAbsolutePath());
					nImages++;
				}
				texturePanels.add(panel);
			}
		}
	}

	private void initComponents() {
		principalViewer = new JPanelViewImage();
		textureCheckBox = new JCheckBox("texture");
		textureBlendedCheckBox = new JCheckBox("blend texture");
		loadTextureButton = new JButton();

		setLayout(null);

		principalViewer.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				principalViewer);
		principalViewer.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 86,
				Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 86,
				Short.MAX_VALUE));

		add(principalViewer);
		principalViewer.setBounds(xStart, 70, x * size, x * size);

		textureCheckBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				textureCheckBoxActionPerformed(evt);
			}
		});
		
		textureBlendedCheckBox.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				textureBlendedCheckBoxActionPerformed(evt);
			}

		});
		add(textureBlendedCheckBox);
		textureBlendedCheckBox.setBounds(104,20, 104, 20);
		add(textureCheckBox);
		textureCheckBox.setBounds(20, 20, 84, 20);

		loadTextureButton.setText("Cargar textura");
		loadTextureButton
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						jButtonLoadTextureActionPerformed(evt);
					}
				});
		add(loadTextureButton);
		loadTextureButton.setBounds(100, yStart + y * size + 50, 120, 20);
		setMaximumSize(new Dimension(200,600));
		setMinimumSize(new Dimension(120,600));
		setPreferredSize(new Dimension(200,600));
	}

	protected void jPanelMousePressed(MouseEvent evt) {
		ImageIcon image = ((JPanelViewImage) evt.getComponent()).getImage();
		String path = ((JPanelViewImage) evt.getComponent()).getPath();
		if (image != null) {
			principalViewer.setImage(image);
			principalViewer.setPath(path);
			principalViewer.repaint();
			setTexture();
		}
	}

	private void jButtonLoadTextureActionPerformed(ActionEvent evt) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		JFileChooser fc = new JFileChooser();
		int returnVal = fc.showDialog(null, "Cargar");
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			Date date = new Date();
			File file = fc.getSelectedFile();
			String name = file.getName();
			int index = name.lastIndexOf(".");
			String suffix = name.substring(index, name.length());
			FileManager.copyFile(file.getAbsolutePath(), texturesFolder
					+ format.format(date) + suffix);
			if (nImages < x * y) {
				texturePanels.get(nImages).createImage(
						texturesFolder + format.format(date) + suffix);
				texturePanels.get(nImages++).repaint();
			} else {
				File oldImage = new File(texturePanels.get(0).getPath());
				oldImage.delete();
				for (int i = 0; i < texturePanels.size() - 1; i++) {
					texturePanels.get(i).setImage(
							texturePanels.get(i + 1).getImage());
					texturePanels.get(i).repaint();
				}
				texturePanels.get(nImages - 1).createImage(
						texturesFolder + format.format(date) + suffix);
				texturePanels.get(nImages - 1).repaint();

			}
			principalViewer.createImage(file.getAbsolutePath());
			principalViewer.repaint();
			setTexture();
		}
	}
	
	private void textureCheckBoxActionPerformed(ActionEvent evt) {
		setTexture();	
	}
	

	private void textureBlendedCheckBoxActionPerformed(ActionEvent evt) {
		setTexture();
	}

	public void addNotifyTexture(NotifyTexture notifyTextureManager) {
		this.notifyTextureManager = notifyTextureManager;
	}
	
	private void setTexture(){
		if(textureCheckBox.isSelected()){
			if(principalViewer.getPath()!= null)
				if(canvas.getGllistener()!=null){
					canvas.getGllistener().setTexture(principalViewer.getPath());
					canvas.getGllistener().setTextureBlended(textureBlendedCheckBox.isSelected());
				}
		
		}else
			canvas.getGllistener().removeTexture();
		notifyTextureManager.launchEvent();
	}
	
	public void setImage(String path){
		principalViewer.createImage(path);
		textureCheckBox.setSelected(true);
		principalViewer.repaint();
	}
	
	public void setTextureBlended(boolean selected){
		System.out.println(selected);
		textureBlendedCheckBox.setSelected(selected);
		principalViewer.repaint();
	}

}